﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRQIDT.functions
{
    public partial class TaskModeling : UserControl
    {
        public TaskModeling()
        {
            InitializeComponent();
            //listview1属性设置
            listView1.Clear();
            listView1.Columns.Add("序号", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段标识", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("执行时间", 100, HorizontalAlignment.Center);
            //listview2属性设置
            listView2.Clear();
            listView2.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
            listView2.Columns.Add("故障标识", 100, HorizontalAlignment.Center);
            listView2.Columns.Add("故障级别", 100, HorizontalAlignment.Center);
            listView2.Columns.Add("故障恢复时间", 100, HorizontalAlignment.Center);
            listView2.Columns.Add("故障发生概率", 100, HorizontalAlignment.Center);
            listView2.Columns.Add("失效率", 100, HorizontalAlignment.Center);
        } 
       
        //新建任务段列表
        private void NewList_Click(object sender, EventArgs e)
        {
            listView1.Clear();            
            listView1.Columns.Add("序号", 50, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段标示", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("执行时间", 100, HorizontalAlignment.Center);        
        }

        //添加任务段
        private void AddList_Click(object sender, EventArgs e)
        {
            SRQIDT.forms.AddTaskSegment ATSform = new SRQIDT.forms.AddTaskSegment();
            ATSform.Show();
        }

        //listview1添加任务段
        public void AddTSListItem(ListViewItem item)
        {
            this.listView1.Items.Add(item);
            this.listView1.Refresh();
        }

        //listview2添加失效模式
        public void AddFMListItem(ListViewItem item)
        {
            this.listView2.Items.Add(item);
            this.listView2.Refresh();
        } 

        //添加失效模式
        private void AddFailureMode_Click(object sender, EventArgs e)
        {
            SRQIDT.forms.AddFailureMode AFMform = new SRQIDT.forms.AddFailureMode();
            AFMform.Show();
        }

        //选择ListView1内容
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView2.Clear();
            if (listView1.SelectedItems.Count > 0)
            {
                //初始化listView2
                listView2.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
                listView2.Columns.Add("故障标识", 100, HorizontalAlignment.Center);
                listView2.Columns.Add("故障级别", 100, HorizontalAlignment.Center);
                listView2.Columns.Add("故障恢复时间", 100, HorizontalAlignment.Center);
                listView2.Columns.Add("故障发生概率", 100, HorizontalAlignment.Center);
                listView2.Columns.Add("失效率", 100, HorizontalAlignment.Center);
                Form1.TS_Name = listView1.SelectedItems[0].SubItems[2].Text;
                int i = Form1.FM_count;
                for (int j = 0; j < i; j++)
                {
                    //任务段名称连接
                    if ((Form1.FMA[j].TS_Name == Form1.TS_Name) && (Form1.FMA[j].FM_isdeleted == false))
                    {
                        ListViewItem LVFM = new ListViewItem();
                        LVFM.Text = Form1.FMA[j].TS_Name;
                        LVFM.SubItems.Add(Form1.FMA[j].FM_id);
                        LVFM.SubItems.Add(Form1.FMA[j].Level.ToString());
                        LVFM.SubItems.Add(Form1.FMA[j].Recovery_time.ToString());
                        LVFM.SubItems.Add(Form1.FMA[j].Probability.ToString());
                        LVFM.SubItems.Add(Form1.FMA[j].FailureRate.ToString());
                        listView2.Items.Add(LVFM);
                    }
                }
                this.DeleteList.Enabled = true;
                this.AddFailureMode.Enabled = true;
                this.DeleteFailureMode.Enabled = true;
            }
            else 
            {
                this.DeleteList.Enabled = false;
                this.AddFailureMode.Enabled = false;
                this.DeleteFailureMode.Enabled = false;
            }

        }

        //删除任务段
        private void DeleteList_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                int k = int.Parse(listView1.SelectedItems[0].SubItems[0].Text) + 1;
                if (k < listView1.Items.Count)
                {
                    for (int j = k; j < listView1.Items.Count; j++)
                    {
                        listView1.Items[j].SubItems[0].Text = (j - 1).ToString();
                    }
                }
                //修改删除标识
                for (int i = 0; i < Form1.TS_count; i++)
                {
                    if (Form1.TSA[i].TS_id == listView1.SelectedItems[0].SubItems[1].Text)
                    {
                        Form1.TSA[i].TS_isdeleted = true;
                        for (int l = 0; l < Form1.FM_count; l++)
                        {
                            if (Form1.FMA[l].TS_Name == listView1.SelectedItems[0].SubItems[2].Text)
                            {
                                Form1.FMA[l].FM_isdeleted = true;
                            }
                        }
                    }
                }
                //正式删除
                listView1.SelectedItems[0].Remove();
            }           
        }

        //删除失效模式
        private void DeleteFailureMode_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                //修改删除标识
                for (int i = 0; i < Form1.FM_count; i++)
                {
                    if (Form1.FMA[i].TS_Name == listView2.SelectedItems[0].SubItems[0].Text)
                    {
                        Form1.FMA[i].FM_isdeleted = true;
                    }
                }
                //正式删除
                listView2.SelectedItems[0].Remove();
            }          
        }
    }
}
